package org.zju.moviemotion.coverflow;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import org.zju.moviemotion.R;

/**
 * This class is an adapter that provides images from a fixed set of resource
 * ids. Bitmaps and ImageViews are kept as weak references so that they can be
 * cleared by garbage collection when not needed.
 * 
 */
public class ResourceImageAdapter extends AbstractCoverFlowImageAdapter {

	/** The Constant TAG. */
	private static final String TAG = ResourceImageAdapter.class
			.getSimpleName();

	/** The Constant DEFAULT_LIST_SIZE. */
	private static final int DEFAULT_LIST_SIZE = 100;

	/** The Constant IMAGE_RESOURCE_IDS. */
	private static final List<Integer> IMAGE_RESOURCE_IDS = new ArrayList<Integer>(
			DEFAULT_LIST_SIZE);
	private int[] RealResourcesPosition;// 实际传递进来的值
	TypedArray movieImages;
	// Drawable drawable = images.getDrawable(0);

	/** The Constant DEFAULT_RESOURCE_LIST. */
	private static int[] DEFAULT_RESOURCE_LIST; // = { R.drawable.image01,
												// R.drawable.image02,
												// R.drawable.image03,
	// R.drawable.image04, R.drawable.image05 };

	/** The bitmap map. */
	private final Map<Integer, WeakReference<Bitmap>> bitmapMap = new HashMap<Integer, WeakReference<Bitmap>>();

	private final Context context;

	BitmapFactory.Options opts = new BitmapFactory.Options();

	/**
	 * Creates the adapter with default set of resource images.
	 * 
	 * @param context
	 *            context
	 */
	public ResourceImageAdapter(final Context context, int[] resourcePositions) {
		super();
		this.context = context;
		opts.inSampleSize = 2;
		setDatas(resourcePositions);
	}

	public void setDatas(int[] resourcePositions) {
		bitmapMap.clear();
		movieImages = context.getResources().obtainTypedArray(
				R.array.movie_images);
		this.RealResourcesPosition = resourcePositions;
		DEFAULT_RESOURCE_LIST = new int[resourcePositions.length];
		for (int i = 0; i < resourcePositions.length; i++) {
			DEFAULT_RESOURCE_LIST[i] = movieImages.getResourceId(
					resourcePositions[i], 0);
		}
		setResources(DEFAULT_RESOURCE_LIST);
		// notifyDataSetChanged();
	}

	/**
	 * Replaces resources with those specified.
	 * 
	 * @param resourceIds
	 *            array of ids of resources.
	 */
	public void setResources(final int[] resourceIds) {
		IMAGE_RESOURCE_IDS.clear();
		for (final int resourceId : resourceIds) {
			IMAGE_RESOURCE_IDS.add(resourceId);
		}
		notifyDataSetChanged();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		return IMAGE_RESOURCE_IDS.size();
		// return RealResourcesPosition.length;
	}

	@Override
	public long getItemId(final int position) {
		return RealResourcesPosition[position];
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pl.polidea.coverflow.AbstractCoverFlowImageAdapter#createBitmap(int)
	 */
	@Override
	protected Bitmap createBitmap(final int position) {
		// if(position<IMAGE_RESOURCE_IDS.size()){

		Log.v(TAG, "creating item " + position);
		final Bitmap bitmap = BitmapFactory.decodeResource(
				context.getResources(), IMAGE_RESOURCE_IDS.get(position), opts); // ((BitmapDrawable)
																					// context.getResources()
		// .getDrawable(IMAGE_RESOURCE_IDS.get(position))).getBitmap();

		bitmapMap.put(position, new WeakReference<Bitmap>(bitmap));

		return bitmap;
	}
}